{% extends "base.html" %}

{% block title %}用户管理 - 后台管理系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">用户管理</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <div class="btn-group me-2">

            <a href="{{ url_for('export_users') }}" class="btn btn-sm btn-success">
                <i class="fas fa-download"></i> 导出用户表格
            </a>
        </div>
    </div>
</div>

<!-- 搜索栏 -->
<div class="row mb-3">
    <div class="col-md-6">
        <form method="GET" class="d-flex">
            <input type="text" class="form-control me-2" name="search" value="{{ search }}" placeholder="搜索用户名、手机号或邮箱...">
            <button type="submit" class="btn btn-outline-secondary">
                <i class="fas fa-search"></i>
            </button>
            {% if search %}
                <a href="{{ url_for('users') }}" class="btn btn-outline-danger ms-2">
                    <i class="fas fa-times"></i>
                </a>
            {% endif %}
        </form>
    </div>
    <div class="col-md-6 text-end">
        <span class="text-muted">共 {{ total }} 个用户</span>
    </div>
</div>

<!-- 用户列表 -->
<div class="card">
    <div class="card-body">
        {% if users %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>用户名</th>
                            <th>手机号</th>
                            <th>邮箱</th>
                            <th>类型</th>
                            <th>年龄</th>
                            <th>性别</th>
                            <th>城市</th>
                            <th>积分</th>
                            <th>等级</th>
                            <th>状态</th>
                            <th>注册时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in users %}
                        <tr>
                            <td>{{ user[0] }}</td>
                            <td>{{ user[1] }}</td>
                            <td>{{ user[2] or '-' }}</td>
                            <td>{{ user[3] or '-' }}</td>
                            <td>
                                {% if user[4] == 0 %}
                                    <span class="badge bg-primary">用户</span>
                                {% elif user[4] == 1 %}
                                    <span class="badge bg-warning">商家</span>
                                {% elif user[4] == 2 %}
                                    <span class="badge bg-danger">禁用</span>
                                {% else %}
                                    <span class="badge bg-secondary">未知</span>
                                {% endif %}
                            </td>
                            <td>{{ user[5] or '-' }}</td>
                            <td>{{ user[6] or '-' }}</td>
                            <td>{{ user[7] or '-' }}</td>
                            <td>{{ user[8] or 0 }}</td>
                            <td>
                                {% set level_name = user[13] %}  <!-- 等级名称在索引12 -->
                                {% if level_name == '追随者' %}
                                    <span class="badge bg-secondary">{{ level_name }}</span>
                                {% elif level_name == '守护者' %}
                                    <span class="badge bg-primary">{{ level_name }}</span>
                                {% elif level_name == '创造者' %}
                                    <span class="badge bg-info">{{ level_name }}</span>
                                {% elif level_name == '真神' %}
                                    <span class="badge bg-warning">{{ level_name }}</span>
                                {% elif level_name == '一言九鼎' %}
                                    <span class="badge bg-danger">{{ level_name }}</span>
                                {% else %}
                                    <span class="badge bg-light text-dark">{{ level_name }}</span>
                                {% endif %}
                            </td>
                            <!-- <td>{{ user[9] or 0 }}</td> -->
                            <td>
                                {% if user[10] == '开启' %}
                                    <span class="badge bg-success">{{ user[10] }}</span>
                                {% elif user[10] == '关闭' %}
                                    <span class="badge bg-warning">{{ user[10] }}</span>
                                {% else %}
                                    <span class="badge bg-danger">{{ user[10] }}</span>
                                {% endif %}
                            </td>
                            <td>{{ user[11].strftime('%Y-%m-%d %H:%M') if user[11] else '-' }}</td>
                            <td>
                                <a href="{{ url_for('user_detail', user_id=user[0]) }}" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            <!-- 分页 -->
            {% if total_pages > 1 %}
                <nav aria-label="用户列表分页">
                    <ul class="pagination justify-content-center">
                        {% if page > 1 %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('users', page=page-1, search=search) }}">
                                    <i class="fas fa-chevron-left"></i>
                                </a>
                            </li>
                        {% endif %}
                        
                        {% for p in range(1, total_pages + 1) %}
                            {% if p == page %}
                                <li class="page-item active">
                                    <span class="page-link">{{ p }}</span>
                                </li>
                            {% elif p <= 3 or p >= total_pages - 2 or (p >= page - 2 and p <= page + 2) %}
                                <li class="page-item">
                                    <a class="page-link" href="{{ url_for('users', page=p, search=search) }}">{{ p }}</a>
                                </li>
                            {% elif p == 4 and page > 6 %}
                                <li class="page-item disabled">
                                    <span class="page-link">...</span>
                                </li>
                            {% elif p == total_pages - 3 and page < total_pages - 5 %}
                                <li class="page-item disabled">
                                    <span class="page-link">...</span>
                                </li>
                            {% endif %}
                        {% endfor %}
                        
                        {% if page < total_pages %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('users', page=page+1, search=search) }}">
                                    <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
            {% endif %}
        {% else %}
            <div class="text-center py-5">
                <i class="fas fa-users fa-3x text-muted mb-3"></i>
                <h5 class="text-muted">暂无用户数据</h5>
                {% if search %}
                    <p class="text-muted">没有找到匹配 "{{ search }}" 的用户</p>
                    <a href="{{ url_for('users') }}" class="btn btn-outline-primary">查看所有用户</a>
                {% else %}
                    <a href="{{ url_for('add_user') }}" class="btn btn-primary">添加第一个用户</a>
                {% endif %}
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}